package com.thinkfit.jy.activity;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.common.lib.bean.VideoBean;
import com.common.lib.bean.VideoSectionBean;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.CourseSectionAdapter;
import com.thinkfit.jy.contract.VideoCourseDetailContract;
import com.thinkfit.jy.presenter.VideoCourseDetailPresenter;
import com.thinkfit.jy.utils.CountDownViewUtils;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.ImageLoadUtils;
import com.thinkfit.jy.utils.LoadViewUtils;
import com.thinkfit.jy.utils.SimpleSettingViewUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;
import com.thinkfit.jy.utils.ToastUtils;
import com.thinkfit.jy.utils.Utils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 视频课程详情
 */
public class VideoCourseInfoActivity extends MainActivity implements VideoCourseDetailContract.View {

    private ArrayList<BaseFragment> mFragments;

    ConstraintLayout clMain,llVideoStart;

    RecyclerView rvCourseContent,rvRankingList;
    ImageView ivIcon;
    LinearLayout llCourseDescription,llDistanceTip;
    TextView tvTitle,tvLevel,tvVideoTime,tvPeopleNum,tvDistance,tvKcal,tvMaxSpeedTip,tvMaxSpeed,tvShare,tvCollect,tvCourseInfo,
    tvArea,tvLanguage,tvUnit;
    private VideoBean mVideo;
    CourseSectionAdapter courseSectionAdapter;
    int mType,vType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_course_info;
    }

    public void init() {
        mVideo = (VideoBean) getIntent().getSerializableExtra(Constants.BUNDLE_EXTRA);
        vType = getIntent().getIntExtra(Constants.BUNDLE_EXTRA_2,1);
        setViewsOnClickListener(R.id.ll_sport_back,R.id.cl_set_item);
        llVideoStart = findViewById(R.id.ll_video_start);
        clMain = findViewById(R.id.cl_main);
        ivIcon = findViewById(R.id.iv_icon);
        tvTitle = findViewById(R.id.tv_title);
        tvLevel = findViewById(R.id.tv_level);
        tvVideoTime = findViewById(R.id.tv_video_time);
        tvPeopleNum = findViewById(R.id.tv_people_num);
        llDistanceTip = findViewById(R.id.ll_distance_tip);
        tvDistance = findViewById(R.id.tv_distance);
        tvKcal = findViewById(R.id.tv_kcal);
        tvMaxSpeedTip = findViewById(R.id.tv_max_speed_tip);
        tvMaxSpeed = findViewById(R.id.tv_max_speed);
        tvShare = findViewById(R.id.tv_share);
        tvCollect = findViewById(R.id.tv_collect);
        tvCourseInfo = findViewById(R.id.tv_course_info);


        tvArea = findViewById(R.id.tv_area);
        tvLanguage = findViewById(R.id.tv_language);
        tvUnit = findViewById(R.id.tv_unit);

        llCourseDescription = findViewById(R.id.ll_course_description);

        rvCourseContent = findViewById(R.id.rv_course_content);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvCourseContent.setLayoutManager(linearLayoutManager);
        rvCourseContent.setAdapter(getCourseAdapter());

        rvRankingList = findViewById(R.id.rv_ranking_list);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rvRankingList.setLayoutManager(linearLayoutManager1);


        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    mType : "+vType);
        if(vType == 1){
            ((VideoCourseDetailContract.Presenter) getPresenter()).getVideoCourseDetail(mVideo.getVideoId(),vType);
        }else {
            ((VideoCourseDetailContract.Presenter) getPresenter()).getLiveVideoDetail(mVideo.getVideoId(),vType);
        }

        tvCollect.setOnClickListener(this);
        tvShare.setOnClickListener(this);
        llVideoStart.setOnClickListener(this);

        Utils.initRegion(tvArea,tvLanguage,tvUnit);
    }

    public CourseSectionAdapter getCourseAdapter(){
        if(courseSectionAdapter == null){
            courseSectionAdapter = new CourseSectionAdapter(getContext());
        }

        return courseSectionAdapter;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cl_set_item:
                SimpleSettingViewUtils.getInstance().initView(getContext(),this);
                break;
            case R.id.ll_sport_back:
//                SportsStatusBarViewUtils.getInstance().isShowTarget(false);
                finish();
                break;
            case R.id.ll_video_start:
                Utils.startSports(getContext(),true);
                break;
            case R.id.tv_share:
                break;
            case R.id.tv_collect:
                if(DataManager.Companion.getInstance().isLogin()){
                    LoadViewUtils.getInstance().initView(getContext(),0);
                    ((VideoCourseDetailContract.Presenter) getPresenter()).addcollect(mVideo.getVideoId(),vType);
                }else {
                    ToastUtils.getInstance().show(getContext(),getString(R.string.app_please_login_first));
                }

                break;

        }
    }

    public void onResume() {
        super.onResume();
        SportsStatusBarViewUtils.getInstance().isMain(false);
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
        initSound();
//        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
//        SportsStatusBarViewUtils.getInstance().isShowTarget(true);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        CountDownViewUtils.getInstance().setIsVideo(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        super.onReceive(map);
        if (map.containsKey(EventBusEvent.TIME_CHANGE)) {
            resetDate();
        }else if(map.containsKey(EventBusEvent.TO_VIDEO)){
            Bundle bundle = new Bundle();
            bundle.putSerializable(Constants.BUNDLE_EXTRA, mVideo);
            bundle.putSerializable(Constants.BUNDLE_EXTRA_2, vType);
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   mVideo : "+mVideo.toString());
            openActivity(vType == 1 ? VideoCourseDetailActivity.class : LiveVideoActivity.class, bundle);
        }else if(map.containsKey(EventBusEvent.VOICE_CHANGE)){
            int i = (int) map.get(EventBusEvent.VOICE_CHANGE);
            if(i <= 0){
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

            }else {
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
            }
        }
    }

    public void initSound(){
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(currentVolume <= 0){
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

        }else {
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
        }
    }

    @NonNull
    @Override
    protected VideoCourseDetailContract.Presenter onCreatePresenter() {
        return new VideoCourseDetailPresenter(this);
    }

    @Override
    public boolean isMainActivity() {
        return false;
    }

    @Override
    public void getVideoCourseDetailSuccess(VideoBean detail) {
        mVideo = detail;
        ImageLoadUtils.getInstance(getContext()).setImageUtlRadius(ivIcon, detail.getCover(), 0,15);
//        BaseUtils.StaticParams.loadImage(getContext(), 0, detail.getCover(), ivIcon);
        tvTitle.setText(detail.getTitle());
        tvLevel.setText(getStrLevel(detail.getStar()));
        tvPeopleNum.setText(getString(R.string.app_sport_video_xx_browse,detail.getBrowse()+""));
//        tvSuitableCroudInfo.setText(Html.fromHtml(detail.getFitman()));
//        tvCourseDescription.setText(Html.fromHtml(detail.getDescribe()));
        tvVideoTime.setText(getString(R.string.app_xx_minutes,detail.getLongtime()+""));
        tvKcal.setText(detail.getKcal()+"");
        tvDistance.setText(getDistance(detail.getVburst()));
        if(detail.getIscoll()){
            tvCollect.setText(getString(R.string.app_collect_success));
        }else {
            tvCollect.setText(getString(R.string.app_collect));
        }
        if(DeviceTypeUtils.getInstance().isTreadmill()){
            tvMaxSpeed.setText(getMaxSpeed(detail.getVburst()));
            tvMaxSpeedTip.setText(getString(R.string.app_max_speed));
            llDistanceTip.setVisibility(View.VISIBLE);
        }else {
            tvMaxSpeedTip.setText(getString(R.string.app_max_slop));
            tvMaxSpeed.setText(getMaxSlop(detail.getVburst()));
            llDistanceTip.setVisibility(View.GONE);
        }
//        tvVideoTrain.setText(Html.fromHtml(detail.getTrain()));
        getCourseAdapter().setNewData(detail.getVburst());
        tvCourseInfo.setText(Html.fromHtml(detail.getDescribe()));

    }

    @Override
    public void getLiveVideoDetailSuccess(VideoBean detail) {
        mVideo = detail;
        ImageLoadUtils.getInstance(getContext()).setImageUtlRadius(ivIcon, detail.getCover(), 0,15);
        tvTitle.setText(detail.getTitle());
        tvLevel.setText(getStrLevel(detail.getStar()));
        tvPeopleNum.setText(getString(R.string.app_sport_video_xx_browse,detail.getBrowse()+""));
        tvVideoTime.setText(getString(R.string.app_xx_minutes,detail.getLongtime()+""));
        tvKcal.setText(detail.getKcal()+"");
        tvDistance.setText(getDistance(detail.getVburst()));
        if(detail.getIscoll()){
            tvCollect.setText(getString(R.string.app_collect_success));
        }else {
            tvCollect.setText(getString(R.string.app_collect));
        }
        if(DeviceTypeUtils.getInstance().isTreadmill()){
            tvMaxSpeed.setText(getMaxSpeed(detail.getVburst()));
            tvMaxSpeedTip.setText(getString(R.string.app_max_speed));
            llDistanceTip.setVisibility(View.VISIBLE);
        }else {
            tvMaxSpeedTip.setText(getString(R.string.app_max_slop));
            tvMaxSpeed.setText(getMaxSlop(detail.getVburst()));
            llDistanceTip.setVisibility(View.GONE);
        }
        getCourseAdapter().setNewData(detail.getVburst());
        tvCourseInfo.setText(Html.fromHtml(detail.getDescribe()));
//        BaseUtils.StaticParams.loadImage(getContext(), 0, detail.getCover(), ivIcon);
//        tvVideoTitle.setText(detail.getTitle());
//        tvVideoBrowse.setText(getString(R.string.app_sport_video_xx_browse,detail.getBrowse()+""));
//        tvSuitableCroudInfo.setText(Html.fromHtml(detail.getFitman()));
//        tvCourseDescription.setText(Html.fromHtml(detail.getDescribe()));
//        tvVideoTime.setText(getString(R.string.app_xx_minutes,detail.getLongtime()+""));
//        tvVideoHeat.setText(getString(R.string.app_xx_kcal,detail.getKcal()+""));
//        tvVideoTrain.setText(Html.fromHtml(detail.getTrain()));

    }

    @Override
    public void addcollectSuccess() {
        if(vType == 1){
            ((VideoCourseDetailContract.Presenter) getPresenter()).getVideoCourseDetail(mVideo.getVideoId(),vType);
        }else {
            ((VideoCourseDetailContract.Presenter) getPresenter()).getLiveVideoDetail(mVideo.getVideoId(),vType);
        }
    }

    public String getDistance(ArrayList<VideoSectionBean> vburst){
        String str = "";
        float distance = 0;
        for (VideoSectionBean bean : vburst){
            float v = bean.getSpeed() / 3600 * 1000;
            float v1 = v * bean.getLongtime();
            distance = distance + v1;
        }
        str = Utils.getNewFloat(distance/1000);
        return str;
    }

    public String getMaxSpeed(ArrayList<VideoSectionBean> vburst){
        String str = "";
        float speed = 0;
        for (VideoSectionBean bean : vburst){
            if(speed < bean.getSpeed()){
                speed = bean.getSpeed();
            }
        }

        str = Utils.getNewFloat(speed);

        return str;
    }

    public String getMaxSlop(ArrayList<VideoSectionBean> vburst){
        String str = "";
        int speed = 0;
        for (VideoSectionBean bean : vburst){
            if(speed < bean.getDrag()){
                speed = bean.getDrag();
            }
        }

        str = Utils.getNewFloat(speed);

        return str;
    }

    public String getStrLevel(int level){
        String str = "";
        switch (level){
            case 1:
                str = getString(R.string.app_video_level_1);
                break;
            case 2:
                str = getString(R.string.app_video_level_2);
                break;
            case 3:
                str = getString(R.string.app_video_level_3);
                break;
            case 4:
                str = getString(R.string.app_video_level_4);
                break;
            case 5:
                str = getString(R.string.app_video_level_5);
                break;

        }
        return str;
    }
}